<script>
    var map;
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();

    
    
    $(function(){
        init_map();
        calcRoute();
    });  
    
    function init_map(){
        directionsDisplay = new google.maps.DirectionsRenderer();
        var center_Paris = new google.maps.LatLng(48.8566140,  	2.3522219);
        var mapOptions = {
            zoom: 12,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            center: center_Paris
        };
        map = new google.maps.Map(document.getElementById("map"), mapOptions);
        directionsDisplay.setMap(map);
    }
    
    function calcRoute() {
        var depart = new google.maps.LatLng(<?php echo $depart->getStationLatitude() ?>, <?php echo $depart->getStationLongitude() ?>);
        var arrive = new google.maps.LatLng(<?php echo $arrive->getStationLatitude() ?>, <?php echo $arrive->getStationLongitude() ?>);
        <?php
            if($repartitions->count()>2){
                $repartitions instanceof TabRepartitionQuery;
                for($i=1;$i<$repartitions->count()-1;$i++){
                    $repartition = $repartitions->get($i);
                    $repartition instanceof TabRepartition;
                    $station = $repartition->getTabStation();
                    echo "
        var point$i = new google.maps.LatLng(".$station->getStationLatitude().", ".$station->getStationLongitude().");";
                }
            }
        ?>
                

        var request = {
            origin: depart,
            destination: arrive,
            // Note that Javascript allows us to access the constant
            // using square brackets and a string value as its
            // "property."
            <?php
                if($repartitions->count()>2){
                    echo "
            waypoints: [";
                    for($i=1;$i<$repartitions->count()-1;$i++){
                        echo "
                {
                    location: point$i,
                    stopover:true
                }";
                        if($i!=$repartitions->count()-2)
                            echo ",";
                    }
                    echo "
                ],";
                }
            ?>
            optimizeWaypoints: true,
            travelMode: google.maps.TravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
    }
</script>
<h1>Itinéraire des répartitions du <?php echo $date->format("d/m/Y") ?></h1>
<div id="map" style="width: 100%; height: 600px;text-align: center">
    <p>Veuillez patienter pendant le chargement de la carte...</p>
</div>